import re
import pandas as pd
from queue import Queue
import random
from http import HTTPStatus
from dashscope import Generation  # 建议dashscope SDK 的版本 >= 1.14.0
from concurrent.futures import ThreadPoolExecutor
# 创建一个队列
intro_queue = Queue()
output_queue = Queue()
def touyi(input_idx_str):
    input_idx = input_idx_str[0]
    input_str = input_idx_str[1]
    print(input_idx)
    messages = [{'role': 'system',
                 'content': '这个工作的技术要求，工作领域，角色定位，工作经验是什么 ？技术要求我只需要列出所有可能需要的技术，工作领域简化成一个短语，角色定位简化成一个短语，工作经验为一个短语。结果尽量简洁。'},
                {'role': 'user', 'content': input_str}]
    response = Generation.call(model="qwen-turbo",
                               messages=messages,
                               # 设置随机数种子seed，如果没有设置，则随机数种子默认为1234
                               seed=random.randint(1, 10000),
                               api_key='sk-005abb5377da47c4a3d40bbc68323235',
                               # 将输出设置为"message"格式
                               result_format='message')
    if response.status_code == HTTPStatus.OK:
        # print(response)
        test_str = response["output"]["choices"][0]["message"]["content"]
        # 定义正则表达式模式
        pattern = re.compile(r"(?:技术要求：(.*?))?[\n]?工作领域：(.*?)[\n]?角色定位：(.*?)[\n]?工作经验：(.*?)(?=\n|$)",
                             re.DOTALL)

        match = pattern.search(test_str)
        if match:
            tech_requirements = match.group(1) if match.group(1) else "无"
            work_field = match.group(2) if match.group(2) else "无"
            role_position = match.group(3) if match.group(3) else "无"
            experience = match.group(4) if match.group(4) else "无"


            output_queue.put((input_idx, tech_requirements, work_field, role_position, experience))

    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))

def url_mul_thread_sele(intro_queue):
    with ThreadPoolExecutor(max_workers=5) as executor:
        while not intro_queue.empty():
                executor.submit(touyi,intro_queue.get())
if __name__ == '__main__':
    combined_df=pd.read_csv('shanghai_java_annual_salary.csv')
    # 遍历DataFrame，将'job_description'列的值及其对应的索引放入队列
    for index, row in combined_df.iterrows():
        intro_queue.put((index, row['job_description']))
    url_mul_thread_sele(intro_queue)
    data_dict = []
    while not output_queue.empty():
        output_data=output_queue.get()
        temp_dict = {
            'Index': output_data[0],
            '技术要求': output_data[1],
            '工作领域': output_data[2],
            '角色定位': output_data[3],
            '工作经验': output_data[4]
        }
        data_dict.append(temp_dict)
    # 将新数据转换为DataFrame
    new_data_df = pd.DataFrame(data_dict)
    new_data_df.set_index('Index', inplace=True, drop=True)
    # 使用merge函数按索引列进行外连接合并
    merged_df = pd.merge(combined_df, new_data_df, how='outer', left_index=True, right_index=True)
    merged_df.to_csv("toyi_shanghai_java_annual_salary.csv", index=False)





